package com.tengju.bff.interfaces.app.common;

import com.tengju.bff.interfaces.app.common.assembler.AppVersionAssembler;
import com.tengju.bff.interfaces.app.common.assembler.UserProtocolAssembler;
import com.tengju.bff.interfaces.app.common.facade.CommonFacade;
import com.tengju.bff.interfaces.app.common.vo.*;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.user.application.protocol.AppVersionService;
import com.tengju.user.application.protocol.UserProtocolService;
import com.tengju.user.domain.model.protocol.AppVersionCheckResult;
import com.tengju.user.domain.model.protocol.ProtocolVersion;
import com.tengju.user.domain.model.protocol.ReqPlatform;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Validated
@RestController
@Api(tags = "app版本")
@RequestMapping(value = "common/")
@Slf4j
public class CommonController {

    private AppVersionService appVersionService;
    private UserProtocolService userProtocolService;
    private CommonFacade commonFacade;

    @Autowired
    public CommonController(AppVersionService appVersionService, UserProtocolService userProtocolService, CommonFacade commonFacade) {
        this.appVersionService = appVersionService;
        this.userProtocolService = userProtocolService;
        this.commonFacade = commonFacade;
    }

    @ApiOperation(value = "校验app版本号", notes = "校验app版本号")
    @RequestMapping(value = "app/checkVersion", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<AppVersionCheckVO> checkAppVersion() {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            String appVersion = servletContext.getVersion();
            String platform = servletContext.getPlatform();

            AppVersionCheckResult result = appVersionService.checkVersion(ReqPlatform.fromValue(platform), appVersion);

            return ApiResponse.newSuccess(AppVersionAssembler.domainToVo(result));

    }

    @ApiOperation(value = "获取用户协议版本号", notes = "获取用户协议版本号")
    @RequestMapping(value = "protocol/getVersion", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<List<ProtocolVersionVO>> getProtoVersion(@RequestBody List<Integer> protocolIds) {
            List<ProtocolVersion> version = userProtocolService.getVersion(
                    UserProtocolAssembler.protocolIdToModel(protocolIds));

            return ApiResponse.newSuccess(UserProtocolAssembler.domainToVo(version));

    }

    @ApiOperation(value = "协议确认", notes = "协议确认")
    @RequestMapping(value = "protocol/agree", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> agreeProto(@RequestBody List<Integer> protocolIds) {
        try {
            return ApiResponse.newSuccess(commonFacade.agreeProto(protocolIds));
        } catch (InterfaceException e) {
            log.error("协议确认接口异常",e);
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }


    }


    @ApiOperation(value = "获取服务协议", notes = "获取服务协议")
    @RequestMapping(value = "service/protocol", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ModelAndView serviceProtocol() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("serviceProtocol.html");
        return modelAndView;

    }

    @ApiOperation(value = "获取隐私政策协议", notes = "获取隐私政策协议")
    @RequestMapping(value = "privacy/protocol", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ModelAndView privacyProtocol() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("privacyProtocol.html");
        return modelAndView;

    }

    @ApiOperation(value = "获取合规协议", notes = "获取合规协议")
    @RequestMapping(value = "rule/protocol", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ModelAndView ruleProtocol() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("ruleProtocol.html");
        return modelAndView;

    }

    @ApiOperation(value = "首页", notes = "首页")
    @RequestMapping(value = "app/homePage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<AppHomePageVO> appHomePage() {
            return ApiResponse.newSuccess(commonFacade.appHomePage());
    }

    @ApiOperation(value = "banner", notes = "banner")
    @RequestMapping(value = "app/banner", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<List<AppBannerVO>> getAppBannerInfo() {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            return ApiResponse.newSuccess(commonFacade.getAppBannerInfo(servletContext.getUserId()));
    }

    @ApiOperation(value = "appStart", notes = "appStart")
    @RequestMapping(value = "app/start", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<AppStartVO> appStart() {
        return ApiResponse.newSuccess(commonFacade.appStart());
    }

    @ApiOperation(value = "日期范围区间列表", notes = "日期范围区间列表")
    @RequestMapping(value = "app/date/range/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<DateRangeVO> getDateRangeList(@RequestParam(value = "key") String key) {

        return ApiResponse.newSuccess(commonFacade.getDateRangeList(key));
    }


}
